package com.lagou.springBootRocketmq.controller;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.lagou.springBootRocketmq.cons.Constant.PAY_TOPIC;

@RestController
@RequestMapping("/api/mq")
public class PayMQController implements SendCallback {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("/pay/send")
    public String send(String msg) {
        Message<String> message = MessageBuilder.withPayload(msg)
                .build();
        Map<String, Object> headers = new HashMap<>();
        headers.put("KEYS", UUID.randomUUID().toString());
        headers.put("pack", "abc");
        headers.put("a", 10);

        // 发送消息
        System.out.println("---->发送消息 " + msg);
        rocketMQTemplate.convertAndSend(PAY_TOPIC + ":tag1", message, headers);

        return "ok";
    }

    @Override
    public void onSuccess(SendResult sendResult) {
    }

    @Override
    public void onException(Throwable throwable) {
    }
}
